package com.huike.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.huike.pojo.Drug;
import com.huike.pojo.Register;
import com.huike.service.DrugService;
import com.huike.service.RegisterService;
import com.huike.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController()
@RequestMapping("drugOut")
public class DrugOutController {
    @Autowired
    private RegisterService registerService;
    @Autowired
    private DrugService drugService;
    //查询病人
    @RequestMapping("getRegisters")
    public ResultVO<PageInfo> getRegisters(Integer id, String registerName,
                                           @RequestParam(value="pageNum",defaultValue = "1") Integer pageNum,
                                           @RequestParam(value="pageSize",defaultValue = "5") Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<Register> registers = registerService.PaginationSearch(id,registerName,"","","");
        PageInfo pageInfo = new PageInfo(registers, 5);
        return new ResultVO<PageInfo>(pageInfo);
    }
    //发药
    @RequestMapping("addDrugOuts")
    public ResultVO<Void> addDrugOut(String id, Integer drugId,Integer number){
        Drug drug = drugService.getDrugsById(drugId);
        String[] strid = id.split(",");
        List<Integer> ids = new ArrayList<>();
        for(String s:strid){
            ids.add(new Integer(s));
        }
        if(ids.size()*number>drug.getResetNumber()) {
            return new ResultVO<>(501, "药品库存不足");
        }
        if(drug.getStatus()!=1){
            return new ResultVO<>(502,"药品不在销售中");
        }
        //发药
        Integer row = 0;
        for (int i:ids){
            for(int j=0;j<number;j++){
               row += drugService.addDrugHospital(drugId,i);
            }
            //减少库存
            drugService.subtractResetNumber(drugId,number);
        }
        //判断药品库存是否为0,为0修改状态
        drug = drugService.getDrugsById(drugId);
        if(drug.getResetNumber()==0){
            drugService.updateInTheSale(drugId);
        }
        //是否发药成功
        if(row>=1){
            return new ResultVO<>(200,"添加成功");
        }
        return new ResultVO<>(500,"添加发送未知异常");
    }

    //查询病人药品信息
    @RequestMapping("getRegisterDrug")
    public ResultVO<PageInfo> getRegisterDrug(Integer id,
                                      @RequestParam(value="pageNum",defaultValue = "1") Integer pageNum,
                                      @RequestParam(value="pageSize",defaultValue = "5") Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<Drug> drugs = drugService.getRegisterDrug(id);
        PageInfo pageInfo = new PageInfo(drugs, 5);
        return new ResultVO<>(pageInfo);
    }

    //查询病人药品数量
    @RequestMapping("getDrugNumbe")
    public Integer getDrugNumbe(Integer id,Integer drugId){
        return drugService.getDrugNumbe(id,drugId);
    }

    //发指定数量的药品
    @RequestMapping("grantDrug")
    public ResultVO<Void> grantDrug(Integer id,Integer drugId,Integer number){
        Integer row= drugService.grantDrug(id,drugId,number);
        if(row==number){
            return new ResultVO<>(200,"给予药品成功");
        }
        return new ResultVO<>(500,"给予药品时发生未知异常");
    }

}
